home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / NTUMIN.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  13KB  |  297 lines

  1. /****************************************************************************
  2.  *
  3.  *      Program name : NTUMIN.C (VER 1.0)
  4.  *
  5.  *    Written By : Eng-Huat Ong and Kian-Mong Low.
  6.  *
  7.  *      This is the main program (startup file). It  allows :
  8.  *          1.    Selection of the various algorithms to used
  9.  *        2.    Options for keyboard data input or data input from a file
  10.  *    It then reads the data from the input file, generates the minterms
  11.  *      and attempt to minimize the function using the selected algorithm.
  12.  *    (All algorithms are variation of the original LOGMIN algorithm.)
  13.  *    It then prints out the input data plus the minimized solution.
  14.  *
  15.  * --------------------------------------------------------------------------
  16.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  17.  *    University.
  18.  *
  19.  *    You are free to use, copy and distribute this software and its
  20.  *    documentation providing that:
  21.  *
  22.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  23.  *
  24.  *        IT IS NOT MODIFIED IN ANY WAY.
  25.  *
  26.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  27.  *
  28.  *    This program is provided "AS IS" without any warranty, expressed or
  29.  *    implied, including but not limited to fitness for any particular
  30.  *    purpose.
  31.  *
  32.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  33.  *    appreciated. Please send to:
  34.  *
  35.  *        Boon-Tiong Tan or Othman Bin Ahmad
  36.  *        School of EEE
  37.  *        Nanyang Technological University
  38.  *        Nanyang Avenue
  39.  *        Singapore 2263
  40.  *        Republic of Singapore
  41.  *
  42.  ***************************************************************************/
  43.  
  44. #include <stdio.h>
  45. #include <time.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <alloc.h>          /* TURBO C ONLY */
  49.  
  50.  
  51. unsigned long mem2;         /* external var */
  52.  
  53. main()
  54.  
  55. {
  56.    unsigned char   option, *readk(), *readf(), *logmin_a(), *logmin_b();
  57.    unsigned char   *logmin_c(), *logmin_d(), *logmin_e(), *logmin_f();
  58.    unsigned char   *logmin_g(), *logmin_h();
  59.    unsigned char   *s, *a, *b, nspm, choice;
  60.    unsigned short  n, mb, ncubes();
  61.    unsigned long   m, mem1;
  62.    void            fprtout(), fprtin(), errsound();
  63.    char            again=1, infile[21], outfile[21], dummy[5];
  64.    FILE            *in, *out;
  65.    clock_t         clock(void), clk1, clk2, clk3;
  66.  
  67. /*** Print Copyright messages ***/
  68. clrscr();
  69. printf("\n\n\n            NTUMIN (LOGIC MINIMIZATION) VER 1.0\n");
  70. printf("                ===================================\n\n");
  71. printf("    Copyright (c) 1992. All Rights Reserved. Nanyang Technological\n");
  72. printf("    University.\n\n\n");
  73.  
  74. printf("    You are free to use, copy and distribute this software and its\n");
  75. printf("     documentation providing that:\n\n");
  76.  
  77. printf("        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.\n\n");
  78.  
  79. printf("        IT IS NOT MODIFIED IN ANY WAY.\n\n");
  80.  
  81. printf("        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.\n\n\n");
  82.  
  83. printf("     This program is provided \"AS IS\" without any warranty, expressed or\n");
  84. printf("     implied, including but not limited to fitness for any particular\n");
  85. printf("     purpose.\n\n");
  86.  
  87. getch();
  88.  
  89.    while (again)                                      /* repeated running */
  90.       {
  91.      mem1 = coreleft();                /* max memory space left */
  92.      mem2 = mem1;                            /* min memory space left */
  93.  
  94.      clrscr();
  95.      printf("                      L O G I C    M I N I M I S A T I O N\n\n");
  96.      printf("                                   U S I N G              \n\n");
  97.      printf("                                  L O G M I N             \n\n\n\n");
  98.      printf("               ╔═════════════════════════════════════════════════╗\n");
  99.      printf("               ║                                                 ║\n");
  100.      printf("               ║                 ALGORITHM MENU                  ║\n");
  101.      printf("               ║                 --------------                  ║\n");
  102.      printf("               ║        1. LOGMIN_A           5. LOGMIN_E        ║\n");
  103.      printf("               ║                                                 ║\n");
  104.      printf("               ║        2. LOGMIN_B           6. LOGMIN_F        ║\n");
  105.      printf("               ║                                                 ║\n");
  106.      printf("               ║        3. LOGMIN_C           7. LOGMIN_G        ║\n");
  107.      printf("               ║                                                 ║\n");
  108.      printf("               ║        4. LOGMIN_D           8. LOGMIN_H        ║\n");
  109.      printf("               ║                                                 ║\n");
  110.      printf("               ╚═════════════════════════════════════════════════╝\n\n\n");
  111.  
  112.      printf("               Please choose one algorithm to run. (1 to 8) -> ");
  113.      scanf("%d", &choice);                              /* algorithm to run */
  114.      while (choice<1 || choice>8)                       /* do until 1 to 8 entered */
  115.         {
  116.            gets(dummy);
  117.            errsound();                                  /* sound alarm */
  118.            printf("               Please choose one algorithm to run. (1 to 8) -> ");
  119.            scanf("%d", &choice);                        /* algorithm to run */
  120.         }
  121.      clrscr();
  122.      printf("\n\n\n\n\n\n");
  123.      printf("               ╔════════════════════════════════════════════╗\n");
  124.      printf("               ║                                            ║\n");
  125.      printf("               ║             MENU FOR DATA ENTRY            ║\n");
  126.      printf("               ║             -------------------            ║\n");
  127.      printf("               ║           1.    Keyboard entry             ║\n");
  128.      printf("               ║                                            ║\n");
  129.      printf("               ║           2.    Data file entry            ║\n");
  130.      printf("               ║                                            ║\n");
  131.      printf("               ║           3.    EXIT                       ║\n");
  132.      printf("               ║                                            ║\n");
  133.      printf("               ╚════════════════════════════════════════════╝\n\n");
  134.  
  135.      printf("\n               Please enter 1, 2 or 3 -> ");           /* prompt user */
  136.      scanf("%d", &option);                         /* user's option */
  137.      while (option !=1 && option !=2 && option !=3)    /* do until 1 to 3 entered */
  138.         {
  139.            gets(dummy);
  140.            errsound();                     /* sound alarm */
  141.            printf("\n               Please enter 1, 2 or 3 -> ");     /* prompt user */
  142.            scanf("%d", &option);                 /* user's option */
  143.         }
  144.  
  145.      if (option == 3)
  146.         exit(0);                                        /* exit to environment */
  147.      else if (option == 2)
  148.         {
  149.            gets(dummy);                                   /* clear keyboard buffer */
  150.            printf("\n               Enter input filename (in.dat) -> ");
  151.            gets(infile);                                  /* get infile */
  152.            if (infile[0]=='\0') strcpy(infile,"in.dat");  /* default as "in.dat" */
  153.  
  154.            printf("\n               Enter output filename (out.dat) -> ");
  155.            gets(outfile);                                     /* get outfile */
  156.            if (outfile[0]=='\0') strcpy(outfile, "out.dat");  /* default as "out.dat" */
  157.         }
  158.      else                                               /* option == 1 */
  159.         {
  160.            printf("\n               Enter number of variables (1 - 255) -> ");
  161.            scanf("%d", &n);                             /* read no. of variables */
  162.            while (n<1 || n>255)                         /* max 255 */
  163.           {
  164.              gets(dummy);
  165.              errsound();
  166.              printf("\n               Enter number of variables (1 - 255) -> ");
  167.              scanf("%d", &n);                       /* read no. of variables */
  168.           }
  169.  
  170.            printf("\nEnter ON array\n--------------\n");
  171.            readk(0,n);                                  /* read & save ON array */
  172.            printf("\nEnter DC array\n--------------\n");
  173.            readk(1,n);                                  /* read & save DC array */
  174.            strcpy(infile, "in.dat");                    /* infile default */
  175.  
  176.            printf("\nEnter output filename (out.dat) -> ");
  177.            gets(dummy);                                      /* clear keyboard buffer */
  178.            gets(outfile);                                    /* get outfile */
  179.            if (outfile[0]=='\0') strcpy(outfile, "out.dat"); /* default as "out.dat" */
  180.         }
  181.  
  182.      if ((in = fopen(infile, "r+")) == NULL)           /* open infile */
  183.         {
  184.            printf("Error opening file, %s\n", infile);
  185.            printf("Program Terminated - 1\n");
  186.            exit(0);
  187.         }
  188.  
  189.      if ((out = fopen(outfile, "a")) == NULL)          /* open outfile */
  190.         {
  191.            printf("Error opening file, %s\n", outfile);
  192.            printf("Program Terminated - 2\n");
  193.            exit(0);
  194.         }
  195.  
  196.      fprintf(out,"ON ARRAY\n");                    /* print labels */
  197.      fprintf(out,"--------\n");
  198.      fprtin(in, out, 0);                           /* print ON-array */
  199.      fprintf(out,"\n");
  200.  
  201.      fprintf(out,"DC ARRAY\n");                     /* print labels */
  202.      fprintf(out,"--------\n");
  203.      fprtin(in, out, 1);                            /* print DC array */
  204.      fprintf(out,"\n");
  205.  
  206.      rewind(in);                             /* reset file pointer */
  207.      clk1 = clock();                         /* timer on */
  208.  
  209.      a = readf(in, a, 0);                    /* read ON array */
  210.      nspm = *(a+3);                          /* no. of byte/minterm */
  211.  
  212.      if (nspm == 0)                          /* check for solution of all 'X' */
  213.         s = a;                               /* transfer solution */
  214.      else
  215.         {
  216.            mb = *(a+2)<<8 | *(a+1);          /* no. of minterms in ON-array */
  217.            m = mb*nspm+4;                    /* memory size required */
  218.  
  219.            b = (unsigned char *) malloc(mb*nspm+4);    /* space for ON-array */
  220.            if (b == 0)
  221.           {
  222.              printf("Out of memory -- LM, *b\n");
  223.              printf("Program terminated - 3\n");
  224.              exit(0);
  225.           }
  226.            memcpy(b, a, m);                       /* make a copy of ON array */
  227.  
  228.            a = readf(in, a, 1);                   /* read DC array  */
  229.            nspm = *(a+3);                         /* no. of bytes/minterm */
  230.  
  231.            fclose(in);                             /* close infile */
  232.  
  233.            if (nspm == 0)                         /* solution of all X or '-' */
  234.           {
  235.              free(b);                         /* free pointer */
  236.              s = a;                           /* transfer solution */
  237.           }
  238.            else
  239.           {
  240.              clk2 = clock();                  /* intermediate timer */
  241.  
  242.              switch (choice)                  /* algorithm to run */
  243.             {
  244.                 case 1 : s = logmin_a(a,b);   /* original LOGMIN */
  245.                      break;
  246.                 case 2 : s = logmin_b(a,b);  /* modification A */
  247.                      break;
  248.                 case 3 : s = logmin_c(a,b);  /* Single phase, mod B */
  249.                      break;
  250.                 case 4 : s = logmin_d(a,b);  /* Single phase, mod C */
  251.                      break;
  252.                 case 5 : s = logmin_e(a,b);  /* Single phase, mod D */
  253.                      break;
  254.                 case 6 : s = logmin_f(a,b);  /* Single phase, mod E */
  255.                      break;
  256.                 case 7 : s = logmin_g(a,b);  /* Single phase, mod F */
  257.                      break;
  258.                 case 8 : s = logmin_h(a,b);  /* Single phase, mod G */
  259.             }
  260.              clk3 = clock();                  /* timer off */
  261.           }
  262.         }
  263.  
  264.      clrscr();
  265.      printf("\nMINIMUM COVER\n");               /* print labels (screen) */
  266.      printf("-------------\n");
  267.  
  268.      fprintf(out,"\nMINIMUM COVER\n");          /* print labels (outfile) */
  269.      fprintf(out,"-------------\n");
  270.      fprtout(s,out);                            /* print solution array */
  271.      fprintf(out,"\n");
  272.      printf("\n");
  273.  
  274.      printf("Minterms generation time = %f seconds\n",(float)(clk2-clk1)/CLK_TCK);
  275.      printf("Actual minimization time = %f seconds\n",(float)(clk3-clk2)/CLK_TCK);
  276.      printf("Cube Count = %d\n", ncubes(s));
  277.      printf("Runtime memory usage = %ld bytes\n\n",mem1-mem2);
  278.      printf("Results obtained running algorithm %d\n\n", choice);
  279.  
  280.      fprintf(out,"Minterms generation time = %f seconds\n",(float)(clk2-clk1)/CLK_TCK);
  281.      fprintf(out,"Actual minimization time = %f seconds\n",(float)(clk3-clk2)/CLK_TCK);
  282.      fprintf(out,"Cube Count = %d\n", ncubes(s));
  283.      fprintf(out,"Runtime memory usage = %ld bytes\n\n",mem1-mem2);
  284.      fprintf(out,"Results obtained running algorithm %d\n\n", choice);
  285.      fprintf(out,"\n==================================================\n");
  286.  
  287.      printf("\007\007");                                  /* run completed alarm */
  288.      printf("Do you want to minimize another array (Y/N)\n");
  289.      scanf(" %c",&again);
  290.      if (again != 'y' && again != 'Y') again = 0;         /* y and Y only */
  291.  
  292.      free(s);                                             /* free pointer */
  293.      fclose(out);                                         /* close files */
  294.      
  295.     }
  296. }
  297.